package com.ovopark.organize.server.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ovopark.organize.server.mapper.FranchiseesRegistrationLogMapper;
import com.ovopark.organize.server.model.entity.FranchiseesRegistrationLog;
import com.ovopark.organize.server.model.vo.FranchiseesRegistrationLogVo;
import com.ovopark.organize.server.service.FranchiseesRegistrationLogService;
import com.ovopark.organize.server.service.LogsService;
import com.ovopark.organize.server.util.CollectionUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Locale;


@Service
public class FranchiseesRegistrationLogServiceImpl extends ServiceImpl<FranchiseesRegistrationLogMapper, FranchiseesRegistrationLog> implements FranchiseesRegistrationLogService {
    Logger logger = LoggerFactory.getLogger(FranchiseesRegistrationLogServiceImpl.class);
    @Resource
    private LogsService logsService;
    @Resource
    private MessageSource messageSource;


    @Override
    public void batchSaveLogs(List<FranchiseesRegistrationLog> logs) {

        if(CollectionUtil.isNotEmpty(logs)){
            for (FranchiseesRegistrationLog log : logs) {
                baseMapper.insert(log);
            }
        }


/*        //拼接 没三千个 为一组
        List<String> valueList = new ArrayList<>();
        List<String> valueObject = new ArrayList<>();
        Integer number = 0;
        Integer totalNumber = logs.size();
        for (FranchiseesRegistrationLog registrationLog : logs) {
            number++;
            String oneValue = "(" + registrationLog.getGroupId() + "," + registrationLog.getFranchiseesId() + ","
                    +'\''+ registrationLog.getFranchiseesName()  +'\'' + ","   +'\''+ registrationLog.getFranchiseesMobilePhone()   +'\''+ ","
                    + registrationLog.getFollowUserId() + ","  +'\'' + registrationLog.getFollowUserName()  +'\'' + ","
                    + registrationLog.getLogId() + ")";
            valueObject.add(oneValue);
            if (number > 3000 || number == totalNumber) {
                valueList.add(StringUtils.join(valueObject, ","));
                valueObject = new ArrayList<>();
                totalNumber = totalNumber - number;
                number = 0;
            }
        }

        for (String organizeUser : valueList) {
            baseMapper.batchSaveLogList(organizeUser);
        }*/


    }

    @Override
    public IPage<FranchiseesRegistrationLogVo> getFranchiseesRegistrationLogById(Page<FranchiseesRegistrationLog> page, Integer logId, Locale locale) {
        IPage<FranchiseesRegistrationLogVo> list = baseMapper.getFranchiseesRegistrationLogById(page, logId);
        if(CollectionUtil.isNotEmpty(list.getRecords())){
            for (FranchiseesRegistrationLogVo log : list.getRecords()) {
                String param = log.getCreateUser()+","+log.getFranchiseesName()+log.getFranchiseesMobilePhone()+","+log.getFollowUserName();
                String description = messageSource.getMessage("franchisees.log", param.split(","), locale);
                log.setDescription(description);
            }

        }


        return list;
    }
}
